package com.bw.utils;

//import com.bw.domain.TbMenu;

import com.bw.domain.TbMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单工具类
 * @author:
 * @date: 2024/7/6 18:27
 **/
public class MenuUtil {
    /***
     * 构建LayUI树形组件数据工具
     * @param allList 所有菜单列表
     * @param menuList 拥有的菜单列表
     *
     * @return
     */
    public static List<TbMenu> treeMenus(List<TbMenu> allList, List<TbMenu> menuList){

        //设置回显
        if(menuList != null) {
            for (TbMenu tbMenu : allList) {
                for (TbMenu menu : menuList) {
                    if(menu.getMenuId().equals(tbMenu.getMenuId())) {
                        // 设置回显
                        tbMenu.setChecked(true);
                    }
                }
            }
        }

        //父级菜单
        List<TbMenu> fatherMenus = new ArrayList<>();
        for(TbMenu menu : allList){
            // 设置所有节点为展开
            menu.setSpread(true);
            if(menu.getPid() == 0){
                fatherMenus.add(menu);
            }
        }

        //构建子级菜单
        for(TbMenu  father : fatherMenus){
            ArrayList<TbMenu> tbMenus = new ArrayList<>();
            father.setChildren(tbMenus);

            for(TbMenu menu : allList){
                if(menu.getPid().equals(father.getMenuId())){
                    father.getChildren().add(menu);
                }
            }
        }
        return fatherMenus;
    }
}
